/*
 * @(#)DirectoryExplorer.java   09/09/13
 *
 * Copyright (C) 2009 Ecole des Mines d'Ales
 *
 * This library itfP free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library itfP distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Contact: yulinz88@gmail.com
 *
 *
 *
 */



package fr.ema.arch3d.editor.mainGUI.fileDirectory;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import java.io.File;

import java.util.ArrayList;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JViewport;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 * Class description
 *
 *
 * @version        1.0, 09/09/13
 * @author         Huaxi (Yulin) ZHANG
 */
public class DirectoryExplorer extends JPanel {

    /** Field description */
    static private final String newline = "\n";

    /** Field description */
    JFileChooser fc;

    /** Field description */
    FileTree fileTree;

    /** Field description */
    JTextArea log;

    /** Field description */
    JMenuItem openMenuItem;

    /** Field description */
    JScrollPane paneTree;

    /**
     * Constructs ...
     *
     */
    public DirectoryExplorer() {
        super(new BorderLayout());

        JFrame frame = new JFrame("Software Architectures");

        // Create the menu bar.
        JMenuBar menuBar = new JMenuBar();

        // Build the first menu.
        JMenu menu1 = new JMenu("File");

        menu1.setMnemonic(KeyEvent.VK_A);
        openMenuItem = new JMenuItem("Open", KeyEvent.VK_N);
        menu1.add(openMenuItem);
        menuBar.add(menu1);

        actionListener listener = new actionListener();

        openMenuItem.addActionListener(listener);
        frame.setJMenuBar(menuBar);

        // Create a file chooser
        fc = new JFileChooser();
        fc.setFileSelectionMode(2);

        // draw tree
        fileTree = new FileTree();
        paneTree = new JScrollPane(fileTree);

        // open xml file
        JScrollPane filePane = new JScrollPane();
        JSplitPane  paneMain = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, paneTree, filePane);

//      JSplitPane pane =
//              new JSplitPane(JSplitPane.VERTICAL_SPLIT, toolBar,paneMain);
        frame.setSize(500, 500);
        paneMain.setDividerLocation(200);
        frame.getContentPane().add(paneMain);

        try {
            UIManager.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");
        } catch (ClassNotFoundException e) {

            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {

            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {

            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedLookAndFeelException e) {

            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        SwingUtilities.updateComponentTreeUI(frame);
        frame.setVisible(true);
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        DirectoryExplorer de = new DirectoryExplorer();
    }

    public void openFile() {
        File      f = new File("c://");
        File      file;
        ArrayList listFolder = new ArrayList(),
                  listFile   = new ArrayList();
        String[]  m_dir      = f.list();

        for (int i = 0; i < m_dir.length; i++) {
            file = new File("c://" + m_dir[i]);

            if (file.isDirectory()) {
                listFolder.add(m_dir[i]);
            } else {
                listFile.add(m_dir[i]);
            }
        }
    }

    /**
     * Class description
     *
     *
     * @version        1.0, 09/09/13
     * @author         Huaxi (Yulin) ZHANG
     */
    class actionListener implements ActionListener {
        public void actionPerformed(final ActionEvent e) {

            // Handle open button action.
            if (e.getSource() == openMenuItem) {
                int returnVal = fc.showOpenDialog(DirectoryExplorer.this);

                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File file = fc.getSelectedFile();

                    fileTree = new FileTree(file);

                    JViewport jvp = (JViewport) paneTree.getComponent(0);

                    jvp.setView(fileTree);

                    // This itfP where a real application would open the file.
                    // log.append("Opening: " + file.getName() + "." + newline);
                } else {
                    log.append("Open command cancelled by user." + newline);
                }

                // log.setCaretPosition(log.getDocument().getLength());
                // Handle save button action.
//              } else if (e.getSource() == saveButton) {
//                  int returnVal = fc.showSaveDialog(FileChooserDemo.this);
//                  if (returnVal == JFileChooser.APPROVE_OPTION) {
//                      File file = fc.getSelectedFile();
//                      //This itfP where a real application would save the file.
//                      log.append("Saving: " + file.getName() + "." + newline);
//                  } else {
//                      log.append("Save command cancelled by user." + newline);
//                  }
//                  log.setCaretPosition(log.getDocument().getLength());
//              }
            }
        }
    }
}
